import { View } from "../../../../mvc/core/View";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { ItemData } from "../../../resource/info/ItemData";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { Cache } from "../../../cache/Cache";
import { ShopType } from "../../../resource/info/ShopType";
import { EBind } from "../../../../../Message/Public/EBind";
import { MaterialItemData } from "../../equipment/MaterialItemData";
import { SPlayerItem } from "../../../../../Message/Public/SPlayerItem";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { DataEvent } from "../../../events/DataEvent";
import { ShopItemInfo } from "../../../resource/info/ShopItemInfo";
import { ColorConfig } from "../../../resource/ColorConfig";
import { Language } from "../../../../../extend/language/Language";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { GameDefConfig } from "../../../resource/GameDefConfig";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { EColor } from "../../../../../Message/Public/EColor";
import { MountUtil } from "../../../scene/mount/MountUtil";
import { MountConfig } from "../../../resource/MountConfig";
import { MsgManager } from "../../../manager/MsgManager";
import { PopupManager } from "../../../manager/PopupManager";
import { PulseSharedObject } from "../../player/PulseSharedObject";
import { MountStartBatchResultPanel } from "../mountStarts/MountStartBatchResultPanel";
import { ObjCreate } from "../../common/ObjCreate";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { ResFileConst } from "../../../resource/ResFileConst";
import { MountStartsMiniAlbum } from "../mountStarts/MountStartsMiniAlbum";
import { MountStartsTargetSp } from "../mountStarts/MountStartsTargetSp";
import { MountStartsDataPart } from "../mountStarts/MountStartsDataPart";
import { MountStartsBallAlbum } from "../mountStarts/MountStartsBallAlbum";
import { MountStartsResultSp } from "../mountStarts/MountStartsResultSp";
import { TilePanel } from "../../equipment/TilePanel";
import { MountWashMaterialCellRenderer } from "../cell/MountWashMaterialCellRenderer";
import { EquipmentModuleConst } from "../../equipment/data/EquipmentModuleConst";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../../resource/ImagesConst";
import { MountStartsEvents } from "../events/MountStartsEvents";
import { GLoadedButton } from "../../../../../com/mui/controls/GLoadedButton";
import { GCheckBox } from "../../../../../com/mui/controls/GCheckBox";
type int = number;
//class MountStartsPanel
    
    export  class MountStartsPanel extends View
    {
        constructor()
        {
            super();
            this._materialPanelData = [];
            this.initView();
            this.addEventListeners();
            return;
        }

        public setStartLv(arg1: int): void
        {
            this._miniAlBum.addImgByIndex(arg1);
            return;
        }

        /* internal  */onAddedToStage(arg1: Object /* flash.events.Event */): void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.updateMaterial);
            return;
        }

        /* internal  */onRemovedFromStage(arg1: Object /* flash.events.Event */): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.updateMaterial);
            return;
        }

        public updateInfo(arg1: ItemData, arg2: Boolean): void
        {
            var loc2=0;
            if (!arg1) 
            {
                this.clean();
                return;
            }
            var loc1=false;
            if (this._mountIdata && !(this._mountIdata.itemExInfo.m_stTa.length == arg1.itemExInfo.m_stTa.length)) 
            {
                loc1 = true;
            }
            this._mountIdata = arg1;
            if (this._mountIdata.itemExInfo.m_haSt != "1") 
            {
                this._specialBm.filters = [FilterConst.colorFilter];
            }
            else 
            {
                this._specialBm.filters = [];
            }
            this._miniAlBum.updateInfo(arg1);
            this._ballAlbum.updateInfo(arg1, arg2);
            this._targetPart.updateInfo(arg1);
            this._resultPart.updateInfo(arg1, arg2);
            this._dataPart.updateInfo(arg1);
            if (arg2 || loc1) 
            {
                loc2 = (arg1.itemExInfo.m_stLv.length - 1);
                this._miniAlBum.addImgByIndex(loc2);
            }
            this._resultPart.update(this._miniAlBum.currentIndex);
            this._dataPart.update(this._miniAlBum.currentIndex);
            this._targetPart.updateSeleted(this._miniAlBum.currentIndex);
            if (this._mountIdata.itemExInfo.m_haSt == "0" || this._mountIdata.itemExInfo.m_haSt == "") 
            {
                this.clean();
            }
            else 
            {
                this._btnLeft.mouseChildren = true;
                this._btnLeft.mouseEnabled = true;
                this._btnRight.mouseChildren = true;
                this._btnRight.mouseEnabled = true;
                this._ballAlbum.mouseChildren = true;
                this._ballAlbum.mouseEnabled = true;
                this._targetPart.mouseEnabled = true;
                this._miniAlBum.filters = [];
                this._btnLeft.filters = [];
                this._btnRight.filters = [];
                this._ballAlbum.filters = [];
                this._targetPart.filters = [];
            }
            return;
        }

        /* internal  */clean(): void
        {
            this._btnLeft.mouseChildren = false;
            this._btnLeft.mouseEnabled = false;
            this._btnRight.mouseChildren = false;
            this._btnRight.mouseEnabled = false;
            this._ballAlbum.clean();
            this._targetPart.mouseEnabled = false;
            this._miniAlBum.filters = [FilterConst.colorFilter];
            this._btnLeft.filters = [FilterConst.colorFilter];
            this._btnRight.filters = [FilterConst.colorFilter];
            this._targetPart.filters = [FilterConst.colorFilter];
            this._dataPart.clean();
            this._resultPart.clean();
            return;
        }

        public updateMaterial(arg1: Object=null): void
        {
            var loc4=null;
            var loc5=null;
            var loc6=0;
            var loc7=0;
            var loc8=null;
            var loc9=null;
            var loc10=0;
            var loc11=null;
            var loc12=null;
            var loc1=Cache.instance;
            var loc2=[];
            var loc3=new Map<any, any> /* flash.utils.Dictionary */();
            loc4 = loc1.equipment.getMaterialData(this._opertionType);
            loc5 = loc1.equipment.getMaterialBuyData(this._opertionType, ShopType.PetForge);
            loc6 = 0;
            while (loc6 < loc4.length) 
            {
                if ((loc9 = loc4[loc6]["data"] as ItemData).itemInfo.item.bind != EBind._EBindYes) 
                {
                    loc10 = loc9.itemInfo.item.code;
                }
                else 
                {
                    loc10 = loc9.itemInfo.item.codeUnbind;
                }
                if (loc10 != 0) 
                {
                    if (loc3[loc10]) 
                    {
                        (loc3[loc10] as MaterialItemData).itemData.itemAmount = (loc3[loc10] as MaterialItemData).itemData.itemAmount + loc9.itemAmount;
                    }
                    else 
                    {
                        (loc8 = new MaterialItemData()).itemData = new ItemData(loc10);
                        loc8.itemData.itemAmount = loc9.itemAmount;
                        loc8.itemData.uid = loc9.uid;
                        loc3[loc10] = loc8;
                    }
                }
                ++loc6;
            }
            loc6 = 0;
            while (loc6 < loc5.length) 
            {
                loc12 = loc5[loc6]["data"];
                if (loc3[loc12.itemInfo.item.code]) 
                {
                    (loc8 = loc3[loc12.itemInfo.item.code] as MaterialItemData).isInShop = true;
                    loc8.shopItem = loc12;
                }
                else 
                {
                    (loc8 = new MaterialItemData()).isInShop = true;
                    loc8.shopItem = loc12;
                    (loc11 = new SPlayerItem()).itemCode = loc12.itemInfo.item.code;
                    loc8.itemData = new ItemData(loc11);
                    loc3[loc11.itemCode] = loc8;
                }
                ++loc6;
            }
            var loc13=0;
            var loc14=loc3;
            for(loc8 of loc14) 
            {
                loc2.push(loc8);
            }
            loc2.sortOn("code");
            this._materialPanelData = loc2;
            this._materialList.dataProvider = new DataProvider(this._materialPanelData);
            return;
        }

        /* internal  */buyMaterialHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data.shopItemInfo as ShopItemInfo;
            var loc2=arg1.data.amount as int;
            var loc3=loc1.tshopSell.offer > 0 ? loc1.tshopSell.offer * loc2 : loc1.tshopSell.price * loc2;
            var loc4=ColorConfig.instance.getItemColor(loc1.itemInfo.item.color);
            var loc5="        " + Language.getStringByParam(61023, HTMLUtil.addColor(loc2 + "", "#FF0000"), HTMLUtil.addColor(loc1.itemInfo.item.name, loc4.color), HTMLUtil.addColor(loc3 + GameDefConfig.instance.getEPrictUnitName(loc1.unit), "#FFEA00"));
            Alert.extendObj = arg1.data;
            Alert.show(loc5, Language.getString(69900), Alert.OK | Alert.CANCEL, null, this.onBuyAlertHanlder);
            return;
        }

        /* internal  */onBuyAlertHanlder(arg1: int, arg2: Object): void
        {
            if (arg1 == Alert.OK) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, arg2));
            }
            return;
        }

        public updateMountStartsResult(arg1: int, arg2: string, arg3: Boolean, arg4: int, arg5: int, arg6: int, arg7: Boolean, arg8: Boolean, arg9: string): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            this._needOpenNextStars = arg7;
            this._tempLv = arg1;
            if (this._mountIdata) 
            {
                loc1 = arg9.split("#");
                if (this._mountIdata.uid == arg2 && this._miniAlBum.currentIndex + 1 == arg1) 
                {
                    this._resultPart.updateMidShowBm(this._miniAlBum.currentIndex + 1, loc1[1], loc1[0]);
                    this._resultPart.playEffect(arg3);
                }
                if (arg3) 
                {
                    loc2 = "";
                    loc3 = "";
                    loc3 = arg1.toString() + "_" + loc1[1] + "_" + loc1[0];
                    if (parseInt(loc1[1]) != EColor._EColorWhite) 
                    {
                        if (parseInt(loc1[1]) != EColor._EColorGreen) 
                        {
                            if (parseInt(loc1[1]) != EColor._EColorBlue) 
                            {
                                if (parseInt(loc1[1]) != EColor._EColorPurple) 
                                {
                                    if (parseInt(loc1[1]) != EColor._EColorOrange) 
                                    {
                                        if (parseInt(loc1[1]) == EColor._EColorRed) 
                                        {
                                            loc2 = Language.getString(70251) + "<font color=\'#F6A660\'>" + Language.getString(70284) + "" + MountUtil.instance.getEjewelName(parseInt(loc1[0])) + " + " + MountConfig.instance.getLvColorAtt2Value(loc3) + "</font>" + Language.getString(70257);
                                        }
                                    }
                                    else 
                                    {
                                        loc2 = Language.getString(70251) + "<font color=\'#FF7902\'>" + Language.getString(70256) + "" + MountUtil.instance.getEjewelName(parseInt(loc1[0])) + " + " + MountConfig.instance.getLvColorAtt2Value(loc3) + "</font>" + Language.getString(70257);
                                    }
                                }
                                else 
                                {
                                    loc2 = Language.getString(70251) + "<font color=\'#ff00ff\'>" + Language.getString(70255) + "" + MountUtil.instance.getEjewelName(parseInt(loc1[0])) + " + " + MountConfig.instance.getLvColorAtt2Value(loc3) + "</font>" + Language.getString(70257);
                                }
                            }
                            else 
                            {
                                loc2 = Language.getString(70251) + "<font color=\'#00a2ff\'>" + Language.getString(70254) + "" + MountUtil.instance.getEjewelName(parseInt(loc1[0])) + " + " + MountConfig.instance.getLvColorAtt2Value(loc3) + "</font>" + Language.getString(70257);
                            }
                        }
                        else 
                        {
                            loc2 = Language.getString(70251) + "<font color=\'#60e71e\'>" + Language.getString(70253) + "" + MountUtil.instance.getEjewelName(parseInt(loc1[0])) + " + " + MountConfig.instance.getLvColorAtt2Value(loc3) + "</font>" + Language.getString(70257);
                        }
                    }
                    else 
                    {
                        loc2 = Language.getString(70251) + "<font color=\'#ffffff\'>" + Language.getString(70252) + "" + MountUtil.instance.getEjewelName(parseInt(loc1[0])) + " + " + MountConfig.instance.getLvColorAtt2Value(loc3) + "</font>" + Language.getString(70257);
                    }
                    MsgManager.showRollTipsMsg(loc2);
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getString(70258));
                }
                if (arg8) 
                {
                    if (arg7) 
                    {
                        this.showOpenNewLvAlert(arg1);
                    }
                }
                else 
                {
                    loc4 = "";
                    if (arg3) 
                    {
                        loc4 = Language.getStringByParam(70259, arg4, arg6, "");
                    }
                    else 
                    {
                        loc4 = Language.getStringByParam(70260, arg4, arg6);
                    }
                    PopupManager.addPopUp(this.batchResultPanel, "window", true);
                    this.batchResultPanel.setResultText(loc4);
                    loc5 = this.localToGlobal(new Object /* flash.geom.Point */(216, 342));
                    this.batchResultPanel.x = loc5.x - this.batchResultPanel.btnOK.x;
                    this.batchResultPanel.y = loc5.y - this.batchResultPanel.btnOK.y;
                }
            }
            return;
        }

        /* internal  */onFlaseBtnOk(arg1: int, arg2: Boolean): void
        {
            if (arg1 == Alert.OK) 
            {
                Alert.resetToDefault();
            }
            if (arg2) 
            {
                PulseSharedObject.save("MountStartsBatchCultureAlert", true);
            }
            return;
        }

        /* internal  */showOpenNewLvAlert(arg1: int): void
        {
            var loc1=MountConfig.instance.getMountStarts(arg1).name;
            var loc2=MountConfig.instance.getMountStarts(arg1 + 1).name;
            Alert.okLabel = Language.getString(69901);
            Alert.buttonWidth = 60;
            Alert.showCloseBtn = true;
            Alert.show(Language.getStringByParam(70261, loc1, loc2), null, Alert.OK, null);
            return;
        }

        public get batchResultPanel(): MountStartBatchResultPanel
        {
            if (!this._batchResultPanel) 
            {
                this._batchResultPanel = new MountStartBatchResultPanel();
                this._batchResultPanel.addEventListener(EventName.MountStartsBantchResultOk, this.onMountStartsBantchResultOk);
            }
            return this._batchResultPanel;
        }

        protected onMountStartsBantchResultOk(arg1: DataEvent): void
        {
            if (this._needOpenNextStars) 
            {
                this.showOpenNewLvAlert(this._tempLv);
            }
            return;
        }

        /* internal  */initView(): void
        {
            ObjCreate.createBg(372, 302, 167, 97, this);
            this._specialBm = ObjCreate.createBitmap("", 0, 0, this);
            addChild(this._specialBm);
            LoaderHelp.addResCallBack(ResFileConst.mountstartsBigPic, this.onLoadBmResource);
            this._miniAlBum = new MountStartsMiniAlbum();
            this._miniAlBum.x = 0;
            this._miniAlBum.y = 0;
            addChild(this._miniAlBum);
            this._targetPart = new MountStartsTargetSp();
            this._targetPart.x = 325;
            this._targetPart.y = 6;
            this.addChild(this._targetPart);
            this._dataPart = new MountStartsDataPart();
            this._dataPart.x = 372;
            this.addChild(this._dataPart);
            this._btnLeft = ObjCreate.createGLoadedButton(ResFileConst.MountStartLeft, 7, 80, 40, 43, this);
            this._btnLeft.name = "_btnLeft";
            this._btnRight = ObjCreate.createGLoadedButton(ResFileConst.MountStartRight, 326, 80, 40, 43, this);
            this._btnRight.name = "_btnRight";
            this._ballAlbum = new MountStartsBallAlbum();
            this._ballAlbum.y = 148;
            addChild(this._ballAlbum);
            this._resultPart = new MountStartsResultSp();
            this._resultPart.y = 214;
            this.addChild(this._resultPart);
            this._autoBuyBox = ObjCreate.createCheckBox(Language.getString(70187), 392, 377, 110, 20, this, "GRadioButton");
            this._autoBuyBox.addEventListener(flash.events.Event.CHANGE, this.onAutoBuyChange);
            this._materialList = new TilePanel();
            this._materialList.x = 380;
            this._materialList.y = 307;
            this._materialList.setSize(165, 71);
            this._materialList.setStyle("cellRenderer", MountWashMaterialCellRenderer);
            this._materialList.addEventListener(EquipmentModuleConst.Event_DoBuyEvent, this.buyMaterialHandler);
            this.addChild(this._materialList);
            this.updateMaterial();
            return;
        }

        /* internal  */onLoadBmResource(): void
        {
            this._specialBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.mountStartsBig_1);
            return;
        }

        protected onAutoBuyChange(arg1: Object /* flash.events.Event */): void
        {
            Cache.instance.pack.mountsPackCache.statrsAutoBuy = this._autoBuyBox.selected;
            return;
        }

        /* internal  */addEventListeners(): void
        {
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddedToStage);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
            this._miniAlBum.addEventListener(MountStartsEvents.TWEENING_COMPLETE, this.onBallAlbumTweenComplete);
            this._ballAlbum.addEventListener(MountStartsEvents.TWEENING_COMPLETE, this.onBallAlbumTweenComplete);
            this._btnLeft.addEventListener(flash.events.MouseEvent.CLICK, this.onAlbumBtnClick);
            this._btnRight.addEventListener(flash.events.MouseEvent.CLICK, this.onAlbumBtnClick);
            return;
        }

        protected onBallAlbumTweenComplete(arg1: MountStartsEvents): void
        {
            var loc1=arg1.target;
            switch (loc1) 
            {
                case this._miniAlBum:
                {
                    this._ballAlbum.updateSeleted(arg1.index);
                    this._targetPart.updateSeleted(arg1.index);
                    this._resultPart.update(arg1.index);
                    this._dataPart.update(arg1.index);
                    break;
                }
                case this._ballAlbum:
                {
                    this._miniAlBum.addImgByIndex(arg1.index);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        protected onAlbumBtnClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (arg1.target.name != "_btnLeft") 
            {
                if (this._mountIdata) 
                {
                    if (this._miniAlBum.currentIndex < (this._mountIdata.itemExInfo.m_stLv.length - 1)) 
                    {
                        this._miniAlBum.addImgByIndex(this._miniAlBum.currentIndex + 1);
                    }
                    else if (this._miniAlBum.currentIndex >= (MountConfig.instance.mapStartsArr.length - 1)) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(70263));
                    }
                    else 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(70250));
                    }
                }
            }
            else 
            {
                this._miniAlBum.addImgByIndex((this._miniAlBum.currentIndex - 1));
            }
            return;
        }

        /* internal */ /* var */_specialBm: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_miniAlBum: MountStartsMiniAlbum;

        /* internal */ /* var */_btnLeft: GLoadedButton;

        /* internal */ /* var */_btnRight: GLoadedButton;

        /* internal */ /* var */_targetPart: MountStartsTargetSp;

        /* internal */ /* var */_ballAlbum: MountStartsBallAlbum;

        /* internal */ /* var */_resultPart: MountStartsResultSp;

        /* internal */ /* var */_dataPart: MountStartsDataPart;

        /* internal */ /* var */_opertionType: int=27;

        /* internal */ /* var */_materialPanelData: Array<any>;

        /* internal */ /* var */_materialList: TilePanel;

        /* internal */ /* var */_autoBuyBox: GCheckBox;

        /* internal */ /* var */_mountIdata: ItemData;

        /* internal */ /* var */_batchResultPanel: MountStartBatchResultPanel;

        /* internal */ /* var */_needOpenNextStars: Boolean;

        /* internal */ /* var */_tempLv: int;
    }
